package com.bw.module_service.ui

import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.module_service.R
import com.bw.module_service.adapter.AnnouncementAdapter
import com.bw.module_service.databinding.ActivityNoticeBinding
import com.bw.module_service.model.AnnouncementModelItem
import com.bw.module_service.viewmodel.ServiceViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.Ui.NoticeActivity)
class NoticeActivity : BaseActivity<ServiceViewModel, ActivityNoticeBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_notice

    private val mViewModel:ServiceViewModel by viewModels()
    private val mAdapter by lazy {
        AnnouncementAdapter()
    }


    override fun initView() {
        super.initView()

    }

    override fun initData() {
        mViewModel.getAnnouncement()
        initAdapter()
    }

    private fun initAdapter() {
        mBinding.rv.adapter = mAdapter
        mBinding.rv.layoutManager = LinearLayoutManager(this)


        mAdapter.setOnItemClickListener(object : AnnouncementAdapter.OnItemClickListener {
            override fun onItemClick(position: Int, item: AnnouncementModelItem) {
                handleItemClick(item)
            }
        })
    }

    private fun handleItemClick(item: AnnouncementModelItem) {
        ARouter.getInstance().build(Router.Ui.PostNoticeActivity).navigation()
    }

    override fun initListener() {
        mViewModel.announcementLiveData_.observe(this){
            val list = it.filter { it.status.isNotEmpty()  }.reversed()
            mAdapter.setData(list)
        }
    }



    override fun onResume() {
        super.onResume()
        mViewModel.getAnnouncement()
    }

}